#!/bin/sh
#
# /etc/rc.d/rc./arpwatch
#
# Start/stop/restart/status arpwatch.

ARPDIR="/var/lib/arpwatch"
IFACE="$2"

OPTIONS="-i $IFACE -f $ARPDIR/arp-$IFACE.dat -u root -e root -s root"

pid="$(ps ax | awk '{if (match($5, ".*/arpwatch$") || $5 == "arpwatch") print $1}')"

start() {
  if [ "$IFACE" = "" ]; then
    echo "Please specify interface name"
    exit 1
  else
    if [ ! -f "$ARPDIR/arp-$IFACE.dat" ]; then
      echo "Creating new database file..."
      touch $ARPDIR/arp-$IFACE.dat
      echo "Starting arpwatch on $IFACE..."
      arpwatch $OPTIONS
    else
      echo "Starting arpwatch on $IFACE..."
      arpwatch $OPTIONS
    fi
  fi
}

stop() {
  echo "Stopping arpwatch..."
  killall arpwatch
}

status() {
  if [ "$pid" != "" ]; then
    echo "arpwatch (pid "$pid") is running..."
  else
    echo "arpwatch is not running..."
  fi
}

case "$1" in
'start')
  start
  ;;
'stop')
  stop
  ;;
'restart')
  stop
  start
  ;;
'status')
  status
  ;;
*)
  echo ""
  echo "Usage: $0 {start [IFACE] | stop | restart [IFACE] | status}"
  echo ""
  exit 1
esac
